<template>
  <div class="layout-container">
    <!-- 左侧导航栏 -->
    <div class="sidebar">
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        background-color="#304156"
        text-color="#fff"
        active-text-color="#ffd04b"
        @select="handleMenuSelect"
      >
        <div class="logo" index="0">
          <span class="logo-text">只此秦旅</span>
        </div>

        <el-menu-item index="1">
          <i class="el-icon-s-home"></i>
          <span>首页</span>
        </el-menu-item>


        <el-menu-item index="11">
          <i class="el-icon-user"></i>
          <span>知识图谱</span>
        </el-menu-item>

        <el-submenu index="4">
          <template #title>
            <i class="el-icon-notebook-2"></i>
            <span>手账生成</span>
          </template>
          <el-menu-item index="4-1">手账制作</el-menu-item>
        </el-submenu>

        <template v-if="isUserLoggedIn && showAdminMenu">
          <el-menu-item index="5">
            <i class="el-icon-document"></i>
            <span>问答对数据管理</span>
          </el-menu-item>
          <el-menu-item index="7">
            <i class="el-icon-location"></i>
            <span>景区信息管理</span>
          </el-menu-item>
          <el-menu-item index="8">
            <i class="el-icon-setting"></i>
            <span>管理员信息</span>
          </el-menu-item>
          <el-menu-item index="12">
            <i class="el-icon-setting"></i>
            <span>用户管理信息</span>
          </el-menu-item>
        </template>

        <template v-if="!showAdminMenu">
          <el-menu-item index="9">
            <i class="el-icon-check"></i>
            <span>提交问答对</span>
          </el-menu-item>
        </template>

        <template v-if="isUserLoggedIn && showAdminMenu ">
          <el-menu-item index="10">
            <i class="el-icon-check"></i>
            <span>审核问答对</span>
          </el-menu-item>
        </template>

        <el-menu-item index="6">
          <i class="el-icon-user"></i>
          <span>个人信息</span>
        </el-menu-item>
      </el-menu>
    </div>

    <!-- 右侧内容区域 -->
    <div class="right-section">
      <!-- 修改顶栏 -->
      <div class="top-bar">
        <div class="welcome-text">
          <template v-if="isUserLoggedIn">
            <router-link
              to="/user-info"
              class="user-link"
              @click="handleUserInfoClick"
            >
              您好，{{ username }}！
            </router-link>
            <button class="logout-button" @click="logout">退出</button>
          </template>
        </div>
      </div>

      <!-- 主要内容区域 -->
      <div class="main-content">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import store from '@/store/index.js';

export default {
  setup() {
    const router = useRouter();
    const activeMenu = ref('1');
    const isUserLoggedIn = computed(() => store.state.isAuthenticated);
    const username = computed(() => store.state.account); // 假设用户名存储在store中
    const showAdminMenu = computed(() => isUserLoggedIn.value && store.state.userRole === 'admin');

    const handleMenuSelect = (index) => {
      activeMenu.value = index;
      switch (index) {
        case '0':
          router.push('/');
          break;
        case '1':
          router.push('/');
          break;
        case '3':
          router.push('/role-setting');
          break;
        case '4-1':
          router.push('/notebook-creation');
          break;
        case '4-2':
          router.push('/notebook-history');
          break;
        case '5':
          router.push('/qa-management');
          break;
        case '7':
          router.push('/scenic-management');
          break;
        case '8':
          router.push('/admin-management');
          break;
        case '12':
          router.push('/user');
          break;
        case '11':
          router.push('/know');
          break;
        case '9':
          router.push('/workflow');
          break;
        case '10':
          router.push('/workflow1');
          break;
        case '6':
          router.push('/user-info');
          break;
      }
    };

    const handleUserInfoClick = () => {
      activeMenu.value = '6';
    };

    const logout = () => {
      console.log('用户点击了退出按钮');
      localStorage.removeItem('userRole');
      store.commit('SET_AUTH_STATUS', false);
      store.commit('SET_USER_ROLE', 'guest');
      store.commit('SET_TOKEN', null);
      localStorage.removeItem('token');
      localStorage.removeItem('userRole');
      router.push('/login');
    };

    return {
      activeMenu,
      handleMenuSelect,
      handleUserInfoClick,
      isUserLoggedIn,
      showAdminMenu,
      username,
      logout,
    };
  },
};
</script>

<style>
html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.layout-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

.sidebar {
  width: 20%;
  height: 100%;
  background-color: #304156;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
}

.right-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.top-bar {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0,21, 41,0.08);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 20px;
}

.welcome-text {
  color: #333;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.main-content {
  flex: 1;
  padding: 20px;
  box-sizing: border-box;
  background-color: #f0f2f5;
  overflow-y: auto;
}

.el-menu {
  border: none !important;
}

.el-menu-vertical {
  border-right: none;
  width: 100%;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-text {
  font-family: KaiTi, '楷体';
  font-size: 20px;
  color: white;
}

.el-menu-item.is-active {
  background-color: #1890ff !important;
}

.user-link {
  color: #333;
  text-decoration: none;
  cursor: pointer;
  margin-right: 10px;
}

.user-link:hover {
  color: #1890ff;
}

.logout-button {
  margin-left: 10px;
  padding: 5px 10px;
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.logout-button:hover {
  background-color: #d32f2f;
}
</style>
